﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using The_LarX.Rendering;

namespace The_LarX
{
    public class PickingColor : Picking
    {
        public RenderTarget2D PickingColorMap
        {
            get { return RenderTexture.RenderTarget; }
        }
        public RenderTarget2D PickinColorManipulatorMap
        {
            get { return RenderTextureManipulators.RenderTarget; }
        }
        private Render2Texture RenderTexture;
        private Render2Texture RenderTextureManipulators;

        private Effect effect;

        public PickingColor()
        {
            RenderTexture = new Render2Texture();
            RenderTextureManipulators = new Render2Texture();

            effect = Engine.Content.Load<Effect>("Engine\\Effects\\fx_Advanced");
        }
        public PickingColor(int width, int height)
        {
            RenderTexture = new Render2Texture(width, height);
            RenderTextureManipulators = new Render2Texture(width, height);

            effect = Engine.Content.Load<Effect>("Engine\\Effects\\fx_Advanced");
        }

        public override void Draw()
        {
            RenderTexture.BeginRender();

            for (int i = 0; i < EngineSystem.Actors.Count; i++)
            {
                if (!ManipulatorActors.Contains(EngineSystem.Actors[i]) &&
                    !ExcludedActors.Contains(EngineSystem.Actors[i]))
                {
                    EngineSystem.Actors[i].DrawPick(Camera, effect);
                }
            }

            RenderTexture.EndRender();
            //RenderTexture.RenderTarget.SaveAsJpeg(new FileStream("C:\\testpick.jpg", FileMode.CreateNew), 640, 360);

            if (ManipulatorActors.Count > 0)
            {
                RenderTextureManipulators.BeginRender();

                for (int i = 0; i < ManipulatorActors.Count; i++)
                {
                    ManipulatorActors[i].DrawPick(Camera, effect);
                }

                RenderTextureManipulators.EndRender();
            }
        }

        public override Actor Pick(int x, int y, bool Manipulator)
        {
            if (RenderTexture.RenderTarget == null ||
                            RenderTexture.RenderTarget.IsDisposed)
                return null;

            if (x > RenderTexture.RenderTarget.Width - 1 ||
                y > RenderTexture.RenderTarget.Height - 1)
                return null;

            if (x <= 0 || y <= 0)
                return null;

            Color Color = Color.Black;

            if (Manipulator && ManipulatorActors.Count > 0)
            {
                Color[] color2 = new Color[1];
                RenderTextureManipulators.RenderTarget.GetData<Color>(0, new Rectangle(x, y, 1, 1), color2, 0, 1);

                for (int i = 0; i < ManipulatorActors.Count; i++)
                {
                    if (ManipulatorActors[i].PickColor == color2[0])
                    {
                        return ManipulatorActors[i];
                    }
                }
            }
            else
            {
                Color[] color = new Color[1];
                RenderTexture.RenderTarget.GetData<Color>(0, new Rectangle(x, y, 1, 1), color, 0, 1);

                Color = color[0];
            }

            Actor found = null;
            for (int i = 0; i < EngineSystem.Actors.Count; i++)
            {
                if (EngineSystem.Actors[i].PickColor == Color)
                {
                    found = EngineSystem.Actors[i];
                }
            }
            return found;
        }
    }
}
